package cc.mrbird.system.controller;

import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.controller.BaseController;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.common.util.FileUtil;
import cc.mrbird.system.domain.Daliy;
import cc.mrbird.system.domain.DaliyDTO;
import cc.mrbird.system.domain.DaliyShowExcel;
import cc.mrbird.system.domain.User;
import cc.mrbird.system.service.DaliyService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

/**
 * 日报的展示
 */
@Controller
public class DaliyShowController extends BaseController {
    /**
     * 日报信息的服务接口
     */
    @Autowired
    private DaliyService daliyService;

    //日志
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Log("获取日报信息")
    @RequestMapping("dailyShow")
    @RequiresPermissions("dailyShow:list")
    public String index() {
        return "system/dailyShow/dailyShow";
    }

    @Log("获取日报展示")
    @RequestMapping("dailyShow/list")
    @RequiresPermissions("dailyShow:list")
    @ResponseBody
    public Map<String, Object> userList(QueryRequest request, DaliyDTO daliyDTO) {
        //数据隔离和搜索的数据
        DaliyDTO daliys = daliyService.dailyWithDept(daliyDTO);
        return super.selectByPageNumSize(request, () -> daliyService.findDailyWithDept(daliys, request));
    }

    /**
     * 日报导出excel
     *
     * @param daliyDTO 搜索的数据
     * @return
     */
    @RequestMapping("dailyShow/excel")
    @ResponseBody
    public ResponseBo userExcel(DaliyDTO daliyDTO) {
        try {
            List<DaliyDTO> list = this.daliyService.dailyShowExcel(daliyDTO);
            return FileUtil.createExcelByPOIKit("日报展示表", list, DaliyShowExcel.class);
        } catch (Exception e) {
            log.error("日报展示表", e);
            return ResponseBo.error("导出Excel失败，请联系网站管理员！");
        }
    }

    @Log("删除日报")
    @RequiresPermissions("dailyShow:delete")
    @RequestMapping("dailyShow/delete")
    @ResponseBody
    public ResponseBo deleteDailyShow(String id) {
        try {
            int states = this.daliyService.deleteDailyShow(id);
            if (states > 0) {
                return ResponseBo.ok("删除日报成功！");
            } else {
                return ResponseBo.ok("删除日报失败！");
            }
        } catch (Exception e) {
            log.error("删除日报失败", e);
            return ResponseBo.error("删除日报失败，请联系网站管理员！");
        }
    }

    /**
     * 修改日报信息
     *
     * @param daliy 修改日报信息
     * @return
     */
    @Log("修改日报")
    @RequestMapping("dailyShow/Update")
    @ResponseBody
    public ResponseBo updateDaliy(Daliy daliy) {
        try {
            //跟新数据
            int states = this.daliyService.updateDaliy(daliy);
            if (states > 0) {
                return ResponseBo.ok("修改日报成功！");
            } else {
                return ResponseBo.ok("修改日报失败！");
            }

        } catch (Exception e) {
            log.error("修改日报失败", e);
            return ResponseBo.error("修改日报失败，请联系网站管理员！");
        }
    }

    /**
     * 判断今天是否添加日报
     *
     * @param daliyDTO 用户的信息
     * @return
     */
    @Log("判断今天是否添加日报")
    @RequestMapping("dailyShow/ifDaliy")
    @ResponseBody
    public ResponseBo ifDaliy(DaliyDTO daliyDTO) {
        try {
            //数据判断
            Boolean states = this.daliyService.ifDaliy(daliyDTO);
            return ResponseBo.ok(states);
        } catch (Exception e) {
            log.error("判断今天日报是否提交失败", e);
            return ResponseBo.error("判断今天日报是否提交失败，请联系网站管理员！");
        }
    }

    /**
     * 判断日期是否补录过日报
     *
     * @param supplementDate 用户的信息
     * @return
     */
    @Log("判断日期是否补录过日报")
    @RequestMapping("dailyShow/ifSupplement")
    @ResponseBody
    public ResponseBo ifSupplement(String supplementDate) {
        try {
            //数据判断
            Boolean states = this.daliyService.ifSupplement(supplementDate);
            return ResponseBo.ok(states);
        } catch (Exception e) {
            log.error("判断日期是否补录过日报是否提交失败", e);
            return ResponseBo.error("判断日期是否补录过日报提交失败，请联系网站管理员！");
        }
    }



    /**
     * 补录日报
     *
     * @param daily                       添加的日报 数组
     * @param executioneFficiency         效率
     * @param reasonsforeFficiencyFailure 效率不及格原因
     * @param supplementDate 补录日期
     */
    @RequestMapping("dailyShow/supplementAdd")
    @ResponseBody
    public ResponseBo present(String daily, String executioneFficiency, String reasonsforeFficiencyFailure,String supplementDate) throws ParseException {
        try {
            //日报信息
            List<DaliyDTO> daliyList = JSONObject.parseArray(daily, DaliyDTO.class);
            //补录日报
            Boolean states = daliyService.supplementAdd(daliyList, executioneFficiency, reasonsforeFficiencyFailure,supplementDate);
            return ResponseBo.ok(states == true ? "补录成功！" : "补录失败");
        } catch (Exception e) {
            log.error("补录日报信息", e);
            return ResponseBo.error("补录日报信息失败，请联系网站管理员！");
        }

    }
}






